/**
 * Copyright 2011 Enjoy Change, Inc. All rights reserved.
 * Use is subject to license terms.
 */
package com.enjoychange.enjoy.web;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

/**
 * 上传文件示例
 * @author Administrator
 * @date May 25, 2011
 */
@Controller
public class UploadFileController {
	// public String listAllBoard(@RequestParam("uploadfile") MultipartFile uploadfile,String username)
    @RequestMapping("/imageUpload.do") //@RequestParam("imageFile") 
    public String listAllBoard(HttpServletRequest request, String username) throws IOException {
    	MultipartFile uploadfile=null;
        System.out.println("-----------------upload.------------:"+username);
        if(uploadfile!=null)
        FileCopyUtils.copy(uploadfile.getBytes(),new File("D:/"+uploadfile.getOriginalFilename()));
     // Check that we have a file upload request
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        //Now we are ready to parse the request into its constituent items. Here's how we do it: 

        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload();

        // Parse the request
        
        FileItemIterator iter;
		try {
			iter = upload.getItemIterator(request);
			 while (iter.hasNext()) {
		            FileItemStream item = iter.next();
		            String name = item.getFieldName();
		            InputStream stream = item.openStream();
		            if (item.isFormField()) {
		                System.out.println("---------Form field " + name + " with value "
		                    + Streams.asString(stream) + " detected.-----------");
		            } else {
		                System.out.println("-------------File field " + name + " with file name "
		                    + item.getName() + " detected.-------------------");
		                
		                // Process the input stream
		              			               
		                FileOutputStream outstream = new FileOutputStream("D:/"+request.getParameter("username"));//打开通道
		    	        FileChannel filechannel=outstream.getChannel();	   
		                
		    	        BufferedOutputStream buout;
		              
		                for (;;) {
		                	byte[] content=new byte[8192];
		                    int res=stream.read(content);//从流中读取固定长度的byte[]	        
				                if (res == -1) {
				                    break;
				                }
				            ByteBuffer contentBuffer=ByteBuffer.wrap(content);
		                	filechannel.write(contentBuffer);//一段一段写入
		                	
		                	contentBuffer.clear();
		             
		                	content=null;
		                }
		    	           
		                filechannel.close();
		    	        
		            }
		        }
		} catch (FileUploadException e) {
			
			e.printStackTrace();
		}	       
       
        
        
        return "upload";
    }
}
